<?php

namespace common\traits;

use Yii;
use common\widgets\phpcsv\CsvExport;
use common\components\office\grid\GridExcel;

/**
 * 下载excel
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
trait DownloadExportTrait {

    /**
     * @inheritdoc
     */
    public function export() {
        ini_set('memory_limit', '512M');
        $this->pagination = false;
        return GridExcel::widget([
            'dataProvider' => $this->search(),
            'asAttachment' => true,
            'protection' => true,
            'title' => $this->exportTitle(),
            'filename' => $this->exportFileName(),
            'captions' => $this->exportCaptionOptions(),
            'columns' => $this->exportFields(),
        ]);
    }

    /**
     * Renders the index view for the module
     * @return string
     */
    public function exportCsv() {
        ini_set('memory_limit', '512M');
        return CsvExport::export([
            'query' => $this->buildQuery(),
            'fileName' => $this->exportFileName(),
            'asAttachment' => true,
            'columns' => $this->exportFields(),
            'headers' => $this->attributeLabels(),
        ]);
    }

}
